package com.fingard.dsp.bank.directbank.bochk01;

import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOCHK01Transaction extends BOCHK01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然日循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                tmpCalendarFrom = tmpClndrStepTo;
                //赋值通用参数
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                BOCHK01Header tmpHeader = new BOCHK01Header();
                tmpHeader.packageId = getQryTransID();
                writeCommonHeader(xmlWriter, tmpHeader);
                XElement tmpXElement = new XElement("AcctStatementREQ", "");
                tmpXElement.addChild(new XElement("AcctNo", p_req7516.actNum));//�~�籼��a
                tmpXElement.addChild(new XElement("Cur", convertCurToBank(p_req7516.actCur)));//���
                tmpXElement.addChild(new XElement("Date", Format.Date10Format2.format(tmpClndrStepTo.getTime())));//日期 YYYY/MM/DD

                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        if ("S".equals(tmpBkReader.getFirstText("/BOCHKB2E/Head/TxStatus"))) {
                            String bankCur = tmpBkReader.getFirstText("/BOCHKB2E/Tx/AcctStatementRLY/Cur");
                            while (tmpBkReader.read("/BOCHKB2E/Tx/AcctStatementRLY/Result/Record")) {
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                tmpRetDetail.actArea = p_req7516.actArea;
                                tmpRetDetail.actNum = p_req7516.actNum;
                                tmpRetDetail.cur = convertCurToATS(bankCur);
                                tmpRetDetail.askNumber = tmpBkReader.readedXElement.getSingleTextNotNull("SeqNo");
                                String transDate = Format.Date10Format.format(Format.Date10Format2.parse(tmpBkReader.readedXElement.getSingleTextNotNull("TxDate")));
                                tmpRetDetail.tranDate = transDate;
                                tmpRetDetail.valueDate = transDate;
                                tmpRetDetail.tranTime = transDate + " 00:00:00";
                                if (tmpBkReader.readedXElement.getSingleText("Particulars") != null) {

                                    tmpRetDetail.abstr = tmpBkReader.readedXElement.getSingleTextNotNull("Particulars/Particular");
                                }
                                tmpRetDetail.comment = tmpBkReader.readedXElement.getSingleTextNotNull("TxDetail");
                                tmpRetDetail.tranAmt = tmpBkReader.readedXElement.getSingleTextNotNull("Amount").replace("-", "");
                                tmpRetDetail.cdFlag = tmpBkReader.readedXElement.getSingleTextNotNull("Amount").contains("-") ? "1" : "2";
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = "错误代码：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode") + "，错误信息：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorDesc");
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }

                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DATE, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7515.reqHeader);
        retAts.actNum = p_req7515.actNum;
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            BOCHK01Header tmpHeader = new BOCHK01Header();
            tmpHeader.packageId = getQryTransID();
            writeCommonHeader(xmlWriter, tmpHeader);
            XElement tmpXElement = new XElement("TodayActivityREQ", "");
            tmpXElement.addChild(new XElement("AcctNo", p_req7515.actNum));//�~�籼��a
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    if ("S".equals(tmpBkReader.getFirstText("/BOCHKB2E/Head/TxStatus"))) {
                        String transDate = Format.Date10Format.format(new Date());
                        String acctCur = convertCurToBank(p_req7515.actCur);
                        while (tmpBkReader.read("/BOCHKB2E/Tx/TodayActivityRLY/Result/Record")) {
                            if (acctCur.toUpperCase().equals(tmpBkReader.readedXElement.getSingleTextNotNull("Cur"))) {//���
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                tmpRetDetail.cur = p_req7515.actCur;
                                tmpRetDetail.askNumber = tmpBkReader.readedXElement.getSingleTextNotNull("SeqNo");
                                tmpRetDetail.tranDate = transDate;
                                tmpRetDetail.valueDate = transDate;
                                tmpRetDetail.tranTime = transDate + " 00:00:00";
                                tmpRetDetail.comment = tmpBkReader.readedXElement.getSingleTextNotNull("Particulars");
                                tmpRetDetail.abstr = tmpBkReader.readedXElement.getSingleTextNotNull("Particulars");
                                tmpRetDetail.tranAmt = tmpBkReader.readedXElement.getSingleTextNotNull("Amount").replace("-", "");
                                tmpRetDetail.cdFlag = tmpBkReader.readedXElement.getSingleTextNotNull("Amount").contains("-") ? "1" : "2";
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "错误代码：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorCode") + "，错误信息：" + tmpBkReader.getFirstText("/BOCHKB2E/Head/ErrorDesc");
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7515.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }
}
